<?php
namespace app\api\controller;

use app\common\controller\Base;
use app\common\exception\FailException;
use app\common\exception\ParameterException;
use think\Request;
use think\Loader;
use think\Log;
use app\common\logic\Recharge as RechargeLogic;
/**
 * @title 充值接口
 * @description 接口说明
 * @group 接口分组
 * @header name:token require:1 default: desc:token
 * @param name:user_type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
 */

class Recharge extends Base
{
    /**
 * @title 测试充值
 * @description 接口说明
 * @author WCJ
 * @url /api/recharge/add_recharge
 * @method POST
 * @param name:money type:int require:1 default:1 other: desc:充值金额
 * @param name:pay_password type:int require:1 default:1 other: desc:支付密码
 * @param name:shopid type:int require:0 default:1 other: desc:商家ID(充值为10w且选择购买股份)
 * @param name:type type:int require:0 default:1 other: desc:1.A型股东，2.B型股东
 *
 * @return id: 分类ID
 * @return name: 分类名称
 * @return pictureid: 分类图标
 *
 */
    public function add_recharge(Request $request)
    {
        $uid=$this->uid;
        $money=$request->post("money");
        $pay_password=$request->post("pay_password");
        $shopid=$request->post("shopid")?$request->post("shopid"):0;
        $type=$request->post("type")?$request->post("type"):0;
        $validate = Loader::validate("User");
        if (!$validate->scene("recharge")->check(['user_id' => $uid,'money'=>$money])) {
            throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()]);
            die();
        }
        if(!$pay_password){
            throw new FailException(['code' => 201,"未填写支付密码不存在","errorCode"=>404]);
        }
        $pay_password=md5($pay_password);
        $datas= RechargeLogic::add_recharge($uid,$money,$pay_password,$shopid,$type);
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;

    }


    /**
     * @title 正式充值
     * @description 接口说明
     * @author WCJ
     * @url /api/recharge/recharge
     * @method POST
     * @param name:money type:int require:1 default:1 other: desc:充值金额
     * @param name:pay_password type:int require:1 default:1 other: desc:支付密码
     * @param name:shopid type:int require:0 default:1 other: desc:商家ID(充值为10w且选择购买股份)
     * @param name:type type:int require:0 default:1 other: desc:1.A型股东，2.B型股东
     * @param name:recharge_type type:int require:0 default:1 other: desc:1.为支付宝 2.为微信 3.为银行卡
     *
     * @return id: 分类ID
     * @return name: 分类名称
     * @return pictureid: 分类图标
     *
     */
    public function recharge(Request $request)
    {   
   
        $uid=$this->uid;
        $money=$request->post("money");
        $pay_password=$request->post("pay_password");
        $shopid=$request->post("shopid")?$request->post("shopid"):0;
        $type=$request->post("type")?$request->post("type"):0;
        $recharge_type=$request->post("recharge_type")?$request->post("recharge_type"):0;
        $validate = Loader::validate("User");
        if (!$validate->scene("recharge")->check(['user_id' => $uid,'money'=>$money])) {
            throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()]);
            die();
        }
  
        $pay_password=md5($pay_password);
        $datas= RechargeLogic::recharge($uid,$money,$pay_password,$shopid,$type,$recharge_type);
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;

    }


    /**
     * @title 充值用户信息
     * @description 接口说明
     * @author WCJ
     * @url /api/recharge/recharge_info
     * @method POST
     *
     * @return id: 分类ID
     * @return name: 分类名称
     * @return pictureid: 分类图标
     *
     */
    public function recharge_info(Request $request)
    {
        $uid=$this->uid;

        $validate = Loader::validate("User");
        if (!$validate->scene("uid")->check(['user_id' => $uid])) {
            throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()]);

        }

        $data= RechargeLogic::get_recharge($uid);
        $datas= ["code"=>200,"msg"=>"成功","data"=>$data];

        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;

    }

    /**
     * @title 充值详细信息
     * @description 接口说明
     * @author WCJ
     * @url /api/recharge/recharge_dt
     * @method POST
     * @param name:reid type:int require:1 default:1 other: desc:充值id
     *
     * @return data: 数据
     *
     */
    public function recharge_dt(Request $request)
    {
        $uid=$this->uid;
        $reid=$request->post("reid");
        $validate = Loader::validate("Common");
        if (!$validate->scene("shopid")->check(['shopid' => $reid])) {
            throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()]);

        }

        $data= RechargeLogic::recharge_dt($uid,$reid);
        $datas= ["code"=>200,"msg"=>"成功","data"=>$data];

        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;

    }

    /**
     * @title 充值记录
     * @description 接口说明
     * @author WCJ
     * @url /api/recharge/recharge_list
     * @method POST
     * @param name:start type:int require:1 default:1 other: desc:开始数
     * @param name:type type:int require:1 default:1 other: desc:充值方式[1,2,3]
     * @param name:num type:int require:1 default:1 other: desc:数量
     * @param name:start_time type:int require:0 default:1 other: desc:开始时间
     * @param name:end_time type:int require:0 default:20 other: desc:结束时间
     *
     * @return data: 数据
     *
     */
    public function recharge_list(Request $request)
    {
        $uid=$this->uid;
        $start=$request->post("start")?$request->post("start"):1;
        $num=$request->post("num")?$request->post("num"):20;
        $type=$request->post("type");
        $start_time = $request->post("start_time");
        $end_time = $request->post("end_time");

        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $uid])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400,  'msg' => $validate->getError()]);
            die();
        }
        $tarray=[];
        if($type) {
            try {
                $tarray = json_decode($type, true);

                if (!is_array($tarray)) {
                    $ret = ['code' => 201, 'errorCode' => 404, 'msg' => '缺少重要参数'];
                    throw new ParameterException($ret);
                }
            } catch (FailException $e) {
                $ret = ['code' => 201, 'errorCode' => 404, 'msg' => '缺少重要参数'];
                throw new ParameterException($ret);
            }
        }

        if (!$validate->scene("date")->check(['date' => $start_time])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new ParameterException($ret);

        }
        if (!$validate->scene("date")->check(['date' => $end_time])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new ParameterException($ret);

        }

        $data= RechargeLogic::recharge_list($uid,$start,$num,$tarray,$start_time,$end_time);
        $datas= ["code"=>200,"msg"=>"成功","data"=>$data[0],"zl"=>$data[1]];

        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;

    }
}